'use strict';//热门讨论
const Controller = require('egg').Controller;
class popular_essaysController extends Controller {
    async index() { //模糊搜索跟分页查询
        const { ctx, app } = this;
        const { keyname, page = 1, number = 10 } = ctx.request.query
        const offset = (page - 1) * number
        let data, count
        if (keyname && page) {
            data = await app.mysql.query(`select id, title,content,user_head,user_name,score from popular_essays where film_type like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from popular_essays where film_type like '%${keyname}%'`)
        } else if (keyname) {
            data = await app.mysql.query(`select id, title,content,user_head,user_name,score from popular_essays where film_type ='%${keyname}%' or film_type  like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from film_type where film_type like '%${keyname}%' or film_type like '%${keyname}%'`)
        } else {
            data = await app.mysql.query(`select id, title,content,user_head,user_name,score from popular_essays limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from popular_essays`)
        }
        ctx.body = {
            code: 0,
            msg: "查找成功",
            data,
            count: count
        }
    }

    async destroy() {
        const { ctx, app } = this;
        const { id } = ctx.params;
        const result = await app.mysql.delete(`popular_essays`, { id: +id })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "删除成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 407,
                msg: "删除失败"
            };
        }
    }
}
module.exports = popular_essaysController;
